package com.dragee.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.dragee.common.P;
import com.dragee.constants.Sms;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @author tys on 2021/7/27
 */
@Component
public class SmsUtil {
    public static String signName;
    public static String accessKeyId;
    public static String accessKeySecret;
    public static com.aliyun.dysmsapi20170525.Client client;


    public static com.aliyun.dysmsapi20170525.Client getClient () {
        if (SmsUtil.client == null) {
            Config config = new Config()
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret);
            config.endpoint = "dysmsapi.aliyuncs.com";
            try {
                client = new com.aliyun.dysmsapi20170525.Client(config);
                return client;
            } catch (Exception e) {
                P.red("短信服务客户端创建失败");
                P.red(e.getMessage());
                return null;
            }
        } else {
            return SmsUtil.client;
        }
    }


    public static String sendCode(String phomeNumbers, String code, String type) throws Exception {

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phomeNumbers)
                .setTemplateCode(Type(type))
                .setSignName(signName)

                //.setTemplateParam("{\"code\":\"1111\"}");
                .setTemplateParam("{\"code\":\""+code+"\"}");
        SendSmsResponse sendSmsResponse = getClient().sendSms(sendSmsRequest);
        SendSmsResponseBody body = sendSmsResponse.getBody();
        return  body.getCode();

    }

     private static String Type(String type){
         String smstype =null;
         switch (type) {
             case "login":
                 smstype = Sms.LOGIN_VERIFICATION_CODE.getTemplateCode();
                 break;
             case "updatePwd":
                 smstype = Sms.PASSWORD_VERIFICATION_CODE.getTemplateCode();
                 break;
             case "bindTel":
                 smstype = Sms.REGISTER_VERIFICATION_CODE.getTemplateCode();
                 break;
         }
           return smstype;
    }

    @Value("${webull.accessKeyId}")
    public  void setAccessKeyId(String accessKeyId) {
       SmsUtil.accessKeyId = accessKeyId;
    }
    @Value("${webull.accessKeySecret}")
    public  void setAccessKeySecret(String accessKeySecret) {
        SmsUtil.accessKeySecret = accessKeySecret;
    }
    @Value("${webull.signName}")
    public  void setSignName(String signName) {
        SmsUtil.signName = signName;
    }

    /*申请签名*/
    public static void main(String[] args)  {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers("15174891101")
                .setTemplateCode(Sms.LOGIN_VERIFICATION_CODE.getTemplateCode())
                .setSignName(signName)
                .setTemplateParam("{\"code\":\"1111\"}");
        // getClient().sendSms(sendSmsRequest);
    }
}
